package com.ravi.controller;

import com.ravi.mapper.PatientMapper;
import com.ravi.mapper.RoomInfoMapper;
import com.ravi.model.query.AddQueueReq;
import com.ravi.model.resp.R;
import com.ravi.queue.QueueUtil;
import com.ravi.queue.StringQueueUtil;
import com.ravi.room.StringRoomUtil;
import com.ravi.service.intf.SendMsgService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@Slf4j
@RequestMapping("/test")
public class TestController {
	@Autowired
	private QueueUtil queueUtil;

	@Autowired
	private StringQueueUtil stringQueueUtil;

	@Autowired
	private StringRoomUtil stringRoomUtil;

	@Autowired
	private PatientMapper patientMapper;

	@Autowired
	private SendMsgService sendMsgService;

	@Autowired
	private RoomInfoMapper roomInfoMapper;

	@PostMapping("add")
	public R enQueue(@RequestBody AddQueueReq addQueueReq) {
		stringQueueUtil.addPatient2Queue(addQueueReq.getRoomCode(), addQueueReq.getCheckupCode());
		String areaCode = roomInfoMapper.getAreaCodeByRoomCode(addQueueReq.getRoomCode());
		sendMsgService.sendMsgToRoomScreen(addQueueReq.getRoomCode());
		sendMsgService.sendMsgToAreaScreen(areaCode);
		sendMsgService.sendMsgToAdminEnd();
		return R.ok("入队成功!");
	}

	@GetMapping("call/{roomCode}")
	public R callPatient(@PathVariable String roomCode) {
		String callPatient = stringRoomUtil.callPatient(roomCode);
		if (StringUtils.isEmpty(callPatient)) {
			return R.error("叫号失败！请重试！");
		} else {
			sendMsgService.sendMsgToSameRoom(roomCode);
			return R.ok();
		}
	}

	@GetMapping("refresh/{roomCode}")
	public R refresh(@PathVariable String roomCode) {
		sendMsgService.sendMsgToSameRoom(roomCode);
		return R.ok("刷新成功");
	}

	@GetMapping("refreshScreen/{roomCode}")
	public R refreshScreen(@PathVariable String roomCode) {
		sendMsgService.sendMsgToRoomScreen(roomCode);
		return R.ok("刷新成功");
	}

	@GetMapping("refreshArea/{areaCode}")
	public void refreshArea(@PathVariable String areaCode) {
		sendMsgService.sendMsgToArea(areaCode);
	}

	@GetMapping("refreshAreaScreen/{areaCode}")
	public R refreshAreaScreen(@PathVariable String areaCode) {
		sendMsgService.sendMsgToAreaScreen(areaCode);
		return R.ok("刷新成功!");
	}

	@GetMapping("refreshGlobal")
	public R refreshGlobal() {
		sendMsgService.sendMsgToAdminEnd();
		return R.ok("刷新成功!");
	}
}
